<template>
  <div>
    <el-table
      ref="table"
      v-loading="dataLoading"
      size="medium"
      :data="tableData"
      :max-height="computedHeight"
      border
      :span-method="objectSpanMethod"
      :header-cell-style="{background:'#00c0ef', color:'#fff',fontWeight:'bold'}"
    >
      <el-table-column prop="type" label="规则类型" width="120">
        <template slot-scope="scope">
          <span class="fc_red">{{scope.row.type}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="case" label="检查类型举例" width="220"></el-table-column>
      <el-table-column prop="content" label="规则示例说明"></el-table-column>
      <el-table-column prop="intro" label="依据" width="180"></el-table-column>
    </el-table>
  </div>
</template>

<script>
import { Options } from '@/utils/constant'
export default {
  data() {
    return {
      dataLoading: false,
      tableData: [],
      columnlist: [
        { name: '规则类型', value: 'type' },
        { name: '检查类型举例', value: 'case' }
      ],
      spanArr: {
        type: [],
        case: []
      }, // 数组合并span
      pos: {
        type: [],
        case: []
      }
    }
  },
  computed: {
    computedHeight() {
      let height = window.innerHeight - 240
      return height
    }
  },

  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      const _data = []
      Options.ruleTypeOptions.forEach(r => {
        r.data.forEach(o => {
          _data.push({
            ...o,
            type: r.label
          })
        })
      })
      this.tableData = _data

      if (this.tableData.length > 0) {
        for (let i = 0; i < this.columnlist.length; i++) {
          this.getSpanArr(this.tableData, this.columnlist[i].value)
        }
      }
      console.log('tableData', this.tableData)
    },
    // 相同行的合并
    getSpanArr(data, column) {
      // 清空
      this.spanArr[column] = []
      this.pos[column] = []

      for (var i = 0; i < data.length; i++) {
        if (i === 0) {
          this.spanArr[column].push(1)
          this.pos[column] = 0
        } else {
          if (column === 'DEP_AREA') {
            if (data[i][column] === data[i - 1][column]) {
              this.spanArr[column][this.pos[column]] += 1
              this.spanArr[column].push(0)
            } else {
              this.spanArr[column].push(1)
              this.pos[column] = i
            }
          } else {
            if (data[i][column] === data[i - 1][column] && data[i].DEP_AREA === data[i - 1].DEP_AREA) {
              this.spanArr[column][this.pos[column]] += 1
              this.spanArr[column].push(0)
            } else {
              this.spanArr[column].push(1)
              this.pos[column] = i
            }
          }
        }
      }
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex >= 0 && columnIndex <= 1) {
        const _row = this.spanArr[column.property][rowIndex]
        const _col = _row > 0 ? 1 : 0
        return {
          rowspan: _row,
          colspan: _col
        }
      }
    }
  }
}
</script>

<style>

</style>
